package com.itboye.eweilisten.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.util.loadingutil.MAlert;
import com.itboye.eweilisten.util.sputil.Const;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler
		 {

	private static final int TIMELINE_SUPPORTED_VERSION = 0x21020001;

	private IWXAPI api;
	private String code;

	public static String DISMISSFRAGMENT = "com.itboye.sunsun.dismiss";
	public static String NotifyPerson = "com.itboye.sunsun.notify.person";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.entry);

		//
		api = WXAPIFactory.createWXAPI(this, Const.APP_ID, false);

		api.handleIntent(getIntent(), this);
		SendAuth.Resp resp = new SendAuth.Resp(getIntent().getExtras());
		code = resp.code;
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);

		setIntent(intent);
		api.handleIntent(intent, this);

	}

	@Override
	public void onReq(BaseReq req) {

		WXEntryActivity.this.finish();

	}

	@Override
	public void onResp(BaseResp resp) {

		this.finish();

		int result = 0;

		code = ((SendAuth.Resp) resp).code;
		// Toast.makeText(getApplicationContext(), code + "hui", 0).show();
		switch (resp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			result = R.string.errcode_success;

			if (resp.transaction.equals("sessionwebpage")) {
				MAlert.alert("分享成功");
				// Intent intent=new Intent(DISMISSFRAGMENT);
				// sendBroadcast(intent);
				this.finish();

			}
			if (resp.transaction.equals("webpage")) {
				MAlert.alert("分享成功");
			}

			// 如果是微信登录


			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			result = R.string.errcode_cancel;
			WXEntryActivity.this.finish();
			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			result = R.string.errcode_deny;
			WXEntryActivity.this.finish();
			break;
		default:
			result = R.string.errcode_unknown;
			WXEntryActivity.this.finish();
			break;
		}

		// Toast.makeText(this, result, Toast.LENGTH_LONG).show();
	}




}